﻿// CWndModuleList.cpp: 实现文件
//

#include "pch.h"
#include "GameHacker.h"
#include "CModuleList.h"
#include "afxdialogex.h"
#include "tlhelp32.h"

// CWndModuleList 对话框

IMPLEMENT_DYNAMIC(CWndModuleList, CDialogEx)

CWndModuleList::CWndModuleList(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MOD_LIST, pParent)
{

}

CWndModuleList::~CWndModuleList()
{
}

void CWndModuleList::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_lstModule);
}

void CWndModuleList::OnCancel()
{
}

void CWndModuleList::OnOK()
{
}


BEGIN_MESSAGE_MAP(CWndModuleList, CDialogEx)
	ON_WM_SIZE()
END_MESSAGE_MAP()


// CWndModuleList 消息处理程序


BOOL CWndModuleList::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//设置listbox的样式
	long lStyle;
	lStyle = GetWindowLongPtr(m_lstModule.m_hWnd, GWL_STYLE);
	lStyle |= LVS_REPORT;
	SetWindowLongPtr(m_lstModule.m_hWnd, GWL_STYLE, lStyle);
	lStyle = m_lstModule.GetExtendedStyle();
	lStyle |= LVS_EX_FULLROWSELECT;//一次选中一整行
	lStyle |= LVS_EX_GRIDLINES;//设置显示格子线
	m_lstModule.SetExtendedStyle(lStyle);

	m_lstModule.InsertColumn(0, L"模块名称", 0, 200);
	m_lstModule.InsertColumn(1, L"基址", 0, 100);
	m_lstModule.InsertColumn(2, L"大小", 0, 100);
	m_lstModule.InsertColumn(3, L"文件位置", 0, 400);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CWndModuleList::GetModList(DWORD dPid)
{
	//处理32位进程， 64位进程用TH32CS_SNAPMODULE32，待扩展
	HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dPid);
	if (hSnap != INVALID_HANDLE_VALUE) {
		MODULEENTRY32 mod{ sizeof(mod) };
		BOOL bMore = Module32First(hSnap, &mod);
		while (bMore) {
			CString txt;
			m_lstModule.InsertItem(0, mod.szModule);
			txt.Format(L"%0X", mod.modBaseAddr);
			m_lstModule.SetItemText(0, 1, txt);
			txt.Format(L"%d", mod.modBaseSize);
			m_lstModule.SetItemText(0, 2, txt);
			m_lstModule.SetItemText(0, 3, mod.szExePath);
			bMore = Module32Next(hSnap, &mod);
		}
	}
	CloseHandle(hSnap);
}


